//
//  MessageViewController.m
//  SimpleNote
//
//  Created by 于洪东 on 2017/11/29.
//  Copyright © 2017年 上海LOL信息技术服务有限公司. All rights reserved.
//

#import "MessageViewController.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>


@interface MessageViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

static NSString * HomeTableViewCellID = @"HomeTableViewCellID";


@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    [SVProgressHUD show];
    AVQuery *query = [AVQuery queryWithClassName:@"Message"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [SVProgressHUD dismiss];
        [self.tableview reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ----  UITableViewDelegate/UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HomeTableViewCellID forIndexPath:indexPath];
    return cell;
}

#pragma mark --- setup UI
-(void)setupUI{
    
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.title = NSLocalizedString(@"Message center", nil);
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(NavigationBarHeight);
    }];
    
}

#pragma mark ---  DZNEmptyDataSetSource/DZNEmptyDataSetDelegate
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"none"];
}

-(NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = NSLocalizedString(@"You haven't received the message for the time being",nil);
    return [[NSAttributedString alloc]initWithString:text attributes:nil];
}

-(void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
}

#pragma mark --- set get

-(UITableView *)tableview{
    if (_tableview == nil) {
        _tableview = [[UITableView alloc]init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:HomeTableViewCellID];
        _tableview.tableFooterView = [[UIView alloc]init];
        
        // 必须setting预估高度才能生效
        _tableview.estimatedRowHeight = 100;
        _tableview.rowHeight = UITableViewAutomaticDimension;
        
        // setting空视图
        _tableview.emptyDataSetSource = self;
        _tableview.emptyDataSetDelegate = self;
    }
    return _tableview;
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray ;
}

@end
